var logistRecordEditCtrl  = new XFApp.client();

XFApp.client.prototype.logistRecordEdit = {
		successCode : 10000,	
		ecommObj : null,
		urlItems : {			
			submitUpdateURL: basePath+"/userCenter/logisticsRecord/saveOrUpdate",
			saveStoreURL: basePath+"/userCenter/logisticsRecord/save",
			submitStoreURL: basePath+"/userCenter/logisticsRecord/submit"
		},
		
		formValidate : function(){
			$("#editForm").validate({
		        rules: {
		        	customerCode: "required",
		        	customerName: "required",
		        	logisticsCode: "required",
		        	logisticsName: "required",
		        	appUId: "required",
		        	appUName: "required",
		        	license:"required"
				  },
				 messages: {
					 customerCode: "请填写客户代码",
			         customerName: "请填写企业名称",
			         logisticsCode: "请填写物流企业代码",
			         logisticsName: "请填写物流企业名称",
			         appUId: "请填写持卡人编号",
			         appUName: "请填写持卡人姓名",
			         license:"营业执照号"
			         }
		    });
			$("#editForm").validate();
		},
		
		event : function (){			
			_this = this;
			
			$("#saveBtn").on("click",function(){				
				if($("#editForm").validate().form()){
					logistRecordEditCtrl.logistRecordEdit.submit(_this.urlItems.saveStoreURL);
				}				
			});
			$("#submitBtn").on("click",function(){				
				if($("#editForm").validate().form()){
					logistRecordEditCtrl.logistRecordEdit.submit(_this.urlItems.submitStoreURL);
				}				
			});
			$("#submitUpdateBtn").on("click",function(){				
				if($("#editForm").validate().form()){
					logistRecordEditCtrl.logistRecordEdit.submit(_this.urlItems.submitUpdateURL);
				}				
			});
			//表单验证
			_this.formValidate();
			
			$("#showBtn").on("click",function(){		
				
				var id = $("#id").val();
				
				if(!id){
					window.webDialogUtil.alert("提示",'请先备案物流企业信息,再查看');
					return ;
				}			
				
				window.location = basePath + "/userCenter/logisticsRecord/showDetail?id="+id+"&type=show" ;
				
			});
			
		},		
		
		
		submit : function (url){		
			
			var params = [];	
			
			$("#inputDiv input[type='radio']").each(function(){
				if($(this).prop("checked")){
					var param = {name:'',value:''};
					param.name=$(this).attr("name");
					param.value= $(this).val();
					params.push(param);					
				}				   
			});
			
			
			$("#inputDiv input[type='text']").each(function() {
				if(!!$(this).attr("name")){
					var param = {name:'',value:''};
					param.name=$(this).attr("name");
					param.value= $(this).val();
					params.push(param);					
				}
			});	
			
			$("#inputDiv input[type='hidden']").each(function() {
				if(!!$(this).attr("name")){
					var param = {name:'',value:''};
					param.name=$(this).attr("name");
					param.value= $(this).val();
					params.push(param);					
				}
			});	
			
			
			var modifyFlag = false;
			if(!!this.ecommObj){
				var entity = {};
				for(var index = 0;index<params.length;index++){
					entity[params[index].name]=params[index].value;
				}
				//没有修过需要上传的文件
				if(!$("#licensePath").val()){
					//输入框内容没有修改
					var flag = false;
					for(var pName in entity){
						if(this.ecommObj[pName]!=entity[pName]){
							flag = true;
							break;
						}
					}
					modifyFlag = !flag;
				}
			}
			if(modifyFlag){
				window.webDialogUtil.alert("提示","请修过后提交");
				return;
			}
			
			//基于ajaxfileUpload.js实现
			$.ajaxFileUpload({
				url : url,
				secureuri : false,
				data:params,
				fileElementId : 'licensePath',//现使用方法
				dataType : 'json',
				success : function(data) {
					if (data.code == _this.successCode) {
						
						webAlert({
			                title:'提示',
			                content: data.msg,
			                opacity:.5,
			                bg:"gray",
			                width:300,
			                height:10,
			                button:[
			                        {
			                        name:"确定",
			                        callback:function(){
			                        	window.location = basePath+"/userCenter/logisticsRecord/index" ;
			                        	this.close();
			                        }
			                        }]
			            });
						
						//window.webDialogUtil.alert("提示",data.msg);	
						//window.location = basePath+"/userCenter/logisticsRecord/index" ;
					} else {
						window.webDialogUtil.alert("提示",data.msg);
					}
				},
				error : function(data, status, e){
					window.webDialogUtil.alert("提示",data.msg);
				}
			});
			
//			$.ajax({
//	    		url :_this.urlItems.updateURL,
//	    		data : params ,
//	    		dataType : "json",
//	    		type : 'post',
//	    		success : function(data) {				
//	    			if(data.code==_this.successCode){ 
//	    				window.webDialogUtil.alert("提示",data.msg);	
//	    			}else{
//	    				window.webDialogUtil.alert("提示",data.msg);
//	    			}
//	    		}
//	    	});
		},
	
		
},

$(function(){	
	var params = [];	
	
	$("#inputDiv input[type='radio']").each(function(){
		if($(this).prop("checked")){
			var param = {name:'',value:''};
			param.name=$(this).attr("name");
			param.value= $(this).val();
			params.push(param);					
		}				   
	});
	
	
	$("#inputDiv input[type='text']").each(function() {
		if(!!$(this).attr("name")){
			var param = {name:'',value:''};
			param.name=$(this).attr("name");
			param.value= $(this).val();
			params.push(param);					
		}
	});	
	
	$("#inputDiv input[type='hidden']").each(function() {
		if(!!$(this).attr("name")){
			var param = {name:'',value:''};
			param.name=$(this).attr("name");
			param.value= $(this).val();
			params.push(param);					
		}
	});	
	var entity = {};
	for(var index = 0;index<params.length;index++){
		entity[params[index].name]=params[index].value;
	}
	logistRecordEditCtrl.logistRecordEdit.ecommObj=entity;
	logistRecordEditCtrl.logistRecordEdit.event();
	
});